package Services;

import java.util.ArrayList;

import Entity.User;
import Services.DBConn.EntityManager;

import javax.servlet.http.HttpServletRequest;

public class WebService {
    private final EntityManager em;
    private User currentUser;
    private String baseURL;
    
    public WebService(){
        this.em = new EntityManager();
        this.setCurrentUser(null);
        this.setBaseURL("/BookStore4280");
    }

    public User getCurrentUser() {
        return currentUser;
    }

    public void init(HttpServletRequest request) {
    	Object temp = request.getSession().getAttribute("userID");
    	
    	if(temp!=null){
	        int userID = Integer.valueOf(temp.toString());
	        
	        this.setCurrentUser(getEntityManager().getUser(userID));
    	}
    }
    
    public void clearUp() {
        this.getEntityManager().getDbConn().closeAll();
    }

	public EntityManager getEntityManager() {
		return em;
	}

	public String getBaseURL() {
		return baseURL;
	}

	public void setBaseURL(String baseURL) {
		this.baseURL = baseURL;
	}

	public void setCurrentUser(User currentUser) {
		this.currentUser = currentUser;
	}
	
	public static boolean isInteger(String s) {
		//http://stackoverflow.com/questions/5439529/determine-if-a-string-is-an-integer-in-java
	    try { 
	        Integer.parseInt(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    return true;
	}

	public static boolean isFloat(String s) {
	    try { 
	        Float.parseFloat(s); 
	    } catch(NumberFormatException e) { 
	        return false; 
	    }
	    return true;
	}
}